package cn.com.dstz.admin.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import cn.com.dstz.base.action.BaseAction;
import cn.com.dstz.utils.ActionUtils;
import cn.com.dstz.utils.FillNameQuery;
import cn.com.dstz.web.constants.Constants;

/**
 * 该类提供删除、列表和预览方法
 * 这些功能只能由管理员进行操作
 * @author 叶装装
 *
 */
public class SuggesstionManagerAct extends BaseAction{
	
	public void list( ){
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		int level = ActionUtils.getAdminLevel( admin );
		String sql = "select * from dstz_suggesstion where 1=1 ";
		Map< String, Object > valueMap = new HashMap< String, Object >( );
		
		if( level > 0 ){
			sql += " and adminid=:adminid";
			valueMap.put( "adminid", admin.get( "id" ) );
		}
		
		this.responseToJsonObject( this.paginationBySQL( sql, null, valueMap, "datetime" )  );
	}
	

	
	public void delete( ){
		String ids = getParameter( "ids" );
		
		if( ids != null ){
			String [] splitIds = StringUtils.split( ids, "," );
			
			if( splitIds != null && splitIds.length > 0 ){
				List< String > deleteList = new ArrayList< String >( );
				
				Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
				int level = ActionUtils.getAdminLevel( admin );
				
				for( String id : splitIds ){
					if( StringUtils.isNotBlank( id )){
						String sql = "delete from dstz_suggesstion where id =" + id;
						if( level > 0 ){
							sql += " and adminid=" + admin.get( "id" );
						}
						deleteList.add( sql );
					}
				}
				
				int del = baseService.saveOrUpdateBatch( deleteList );
				this.addLog( del, "suggesstion", 2, ids );
			}
		}
	}
	
	public String toview( ){
		if( id == null || id == 0 ){
			alertMsg( null );
			return "view";
		}
		String sql = "select * from dstz_suggesstion where id =:id";
		
		Map< String, Object > suggesstion = baseService.queryForMap( sql, FillNameQuery.fillValue( sql, id ) );
		getRequest( ).setAttribute( "data", suggesstion );
		
		return "view";
	}
	
	private static final long serialVersionUID = 1L;

	private Integer id;

	public Integer getId( ) {
		return id;
	}

	public void setId( Integer id ) {
		this.id = id;
	}
	
	
}
